import json
import random

import redis
from rest_framework import generics
from zuoye1.models import *
from zuoye1.serializers import *
from rest_framework.views import APIView,Response


class UserView(APIView):  # 注册
    def post(self, request):
        data = json.loads(request.body)
        mobile = data.get('mobile')
        code = data.get('code')
        name = data.get('name')
        password = data.get('password')
        res = redis.Redis()
        rds_code = res.get(mobile).decode('utf8')
        res.close()
        if rds_code == code:
            try:
                User.objects.create(name=name, password=password, mobile=mobile)
            except Exception as e:
                print(e)
                return Response({'msg': "信息有误", 'code': 204})
            return Response(status=200)
        return Response(data='验证码已过期', status=204)


class code_get(APIView):
    def post(self, request):
        data = json.loads(request.body)
        mobile = data.get('mobile')
        code = random.randint(1000, 9999)
        res = redis.Redis()
        res.set(mobile, code, 60)
        res.close()
        return Response(code)


class UserGet(APIView):        # 登录
    def post(self, request):
        data = json.loads(request.body)
        name = data.get('name')
        password = data.get('password')
        user = User.objects.get(name=name, password=password)
        ser = UserSerializer(user)
        return Response(ser.data, status=200)

    def get(self, request):
        name = request.data.get('name')
        id = request.data.get('id')
        user = User.objects.get(id=id, name=name)
        ser = UserSerializer(user)
        return Response(ser.data)

    def put(self, request):
        id = request.data.get('id')
        name = request.data.get('name')
        img = request.data.get('img')
        username = request.data.get('username')
        if username:
            user = User.objects.filter(id=id, name=name).update(name=username, img=img)
            return Response(data='修改成功')
        user = User.objects.filter(id=id, name=name).update(img=img)
        return Response(data='修改成功')


class User_Address(APIView):
    def get(self, request):
        id = request.data.get('id')
        user_address = UserAddress.objects.filter(User_Address=id)
        ser = UserAddressSerializer(user_address, many=True)
        print(ser.data)
        return Response(ser.data.get('address_ss'))

    def post(self, request):
        id = request.data.get('id')
        address = request.data.get('address')
        UserAddress.objects.create(User_address=id, address_ss=address)
        return Response(data='添加地址成功')

    def put(self, request):
        id = request.data.get('id')
        address1 = request.data.get('address1')
        address = request.data.get('address')
        UserAddress.objects.filter(id=id, User_address=address).update(User_address=address1)
        return Response(data='修改收货地址成功')

    def delete(self, request):
        id = request.data.get('id')
        UserAddress.objects.filter(id=id).delete()
        return Response(data='删除收货地址成功')


